//
//  EFSegmentItem.m
//  MobileView
//
//  Created by 董艳武 on 2018/11/9.
//  Copyright © 2018 Javor Feng. All rights reserved.
//

#import "EFSegmentItem.h"
#import "ThemeKit.h"
#import "Constant.h"
#import "Masonry.h"
#import "EFMessageService.h"
#import "EnvironmentVariable.h"
#import "EFChatListModel.h"
#import "JFLanguageManager.h"
#import "EFSegmentItem+Theme.h"

@implementation EFSegmentItem

- (id)initWithFrame:(CGRect)frame index:(NSInteger)index stu:(StubObject *)stu norColor:(UIColor *)norColor selColor:(UIColor *)selColor isSelected:(BOOL)isSelected  {
    self = [super initWithFrame:frame];
    if (self) {
        self.stubObject      = stu;
        self.index           = index;
        self.stubObject      = stu;
        self.theme_norColor = [UIColor theme_colorForKey:@"ordinaryColor" from:@"segment"];
        self.theme_selColor = [UIColor theme_colorForKey:@"selectedColor" from:@"segment"];
        [self createViewsWithSelected:isSelected];
        [self addService];
        [self addNoti];
    }
    return self;
}

-(void)createViewsWithSelected:(BOOL)isSelected {
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.backgroundColor = [UIColor clearColor];
    self.titleLabel.clipsToBounds = YES;
    self.titleLabel.layer.cornerRadius = kBorderLineWidth;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:self.titleLabel];
    
    self.badgeView = [TagView new];
    self.badgeView.sencondMenu = YES;
    self.badgeView.layer.cornerRadius = 9;
    self.badgeView.badgeValue = [self unreadCountWithStubobject:self.stubObject];
    [self addSubview:_badgeView];
    [self.badgeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(18, 18));
        make.top.equalTo(self.mas_top).with.offset(2);
        make.right.equalTo(self.mas_right).with.offset(0);
    }];
    
    self.moreImage = [[UIImageView alloc]init];
    [self addSubview:self.moreImage];
    [_moreImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).with.offset(-2);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.mas_equalTo(self);
    }];
    self.moreImage.userInteractionEnabled = YES;
    self.moreImage.hidden = YES;
    UIImage *moreImage = BundleImage(@"ChatComps.bundle", @"/image_find/more.png");
    moreImage = [moreImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    self.moreImage.image = moreImage;
    self.moreImage.theme_tintColor = [UIColor theme_colorForKey:@"arrowColor" from:@"Zone"];
    
    self.titleLabel.text = [JFLanguageManager titleWithStubobject:self.stubObject];
    self.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.isSelected = isSelected;
}

-(void)addService {
    //将关联apply的cell添加到EFMessageService的ViewMap中
    if ([self.stubObject getStringForKey:@"applyId" withDefaultValue:nil]) {
        NSMutableArray *sonArray = [[NSMutableArray alloc]init];
        if ([[EFMessageService sharedMessageService].viewMap objectForKey:[self.stubObject getStringForKey:@"applyId" withDefaultValue:nil]]) {
            sonArray = [[EFMessageService sharedMessageService].viewMap objectForKey:[self.stubObject getStringForKey:@"applyId" withDefaultValue:nil]];
        }
        [sonArray addObject:self];
        [[EFMessageService sharedMessageService].viewMap setObject:sonArray forKey:[self.stubObject getStringForKey:@"applyId" withDefaultValue:nil]];
    }
    if ([[self.stubObject getStringForKey:@"more" withDefaultValue:@"0"]boolValue]) {
        _moreImage.hidden = NO;
    }
}

-(void)addNoti {
    if([self.stubObject getStringForKey:@"pusuType" withDefaultValue:@""]) {
        NSString *pushType = [self.stubObject getStringForKey:@"pusuType" withDefaultValue:@""];
        if([pushType isEqualToString:NEWS_PUSH]) {
            //监听新闻推送
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(showRedPoint)
                                                         name:PUSH_NEWS
                                                       object:nil];
            
            //资讯刷新
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(hideRedPoint)
                                                         name:NEWS_END_REFRESH
                                                       object:nil];
        }
    }
}

-(void)moreAction {
    if ([[self.stubObject getStringForKey:@"more" withDefaultValue:@"0"]boolValue]) {
        if (_delegate && [_delegate respondsToSelector:@selector(segmentMoreMenuDidSelectWithIndex:stubobject:segmentFrame:)]) {
            [_delegate segmentMoreMenuDidSelectWithIndex:self.index stubobject:self.stubObject segmentFrame:self.frame];
            NSLog(@"------%f---%f---%f---%f",self.frame.size.width,self.frame.size.height,self.frame.origin.x,self.frame.origin.y);
        }
    }
}

#pragma mark -IBadgeView
-(void)badgePlusOne {
    self.badgeView.badgeValue = [NSString stringWithFormat:@"%ld",[self.badgeView.badgeValue integerValue]+1];
}

-(void)badgeMinusOne {
    self.badgeView.badgeValue = [NSString stringWithFormat:@"%ld",[self.badgeView.badgeValue integerValue]-1];
}

-(void)badgeChange:(NSInteger)count {
    self.badgeView.badgeValue = [NSString stringWithFormat:@"%ld",count];
}

//获取list
-(NSString *)unreadCountWithStubobject:(StubObject *)stub {
    //XML上配置的一些APPLYID总共的未读数目
    NSArray  *appArray = [[stub getStringForKey:@"applyId" withDefaultValue:nil] componentsSeparatedByString:@";"];
    NSInteger applyCount = 0;
    if (appArray) {
        //根据EFMessageService的menuMap 赋值badgevalue
        for (NSString *applyID in appArray) {
            applyCount += [[[EFMessageService sharedMessageService].menuMap objectForKey:applyID] integerValue];
        }
    }
    //特殊处理的一些未读数目
    NSInteger listCount = 0;
    if ([[stub.stubTable objectForKey:@"show"] isEqualToString:@"EFChatListViewController"]) {
        //是聊天列表
        listCount = [[EnvironmentVariable getPropertyForKey:@"EFChatListViewController" WithDefaultValue:@"0"] integerValue];
        if ((listCount + applyCount)>99) {
            return @"99+";
        }
        return [NSString stringWithFormat:@"%ld",listCount+applyCount];
    }else if([[stub.stubTable objectForKey:@"show"] isEqualToString:@"EFTaskListViewController"]){
        listCount = [[EnvironmentVariable getPropertyForKey:@"EFTaskListViewController" WithDefaultValue:@"0"] integerValue];
        if ((listCount + applyCount)>99) {
            return @"99+";
        }
        return [NSString stringWithFormat:@"%ld",listCount+applyCount];
    }else {
        //电话
    }
    return @"0";
}

- (void)setSelColor:(UIColor *)selColor {
    if (_selColor != selColor) {
        _selColor = selColor;
        if (_isSelected) {
            self.titleLabel.textColor = self.norColor;
            self.backgroundColor = self.selColor;
        } else {
            self.titleLabel.textColor = self.selColor;
            self.backgroundColor = self.norColor;
        }
    }
}

-(void)setNorColor:(UIColor *)norColor {
    _norColor = norColor;
    if (_isSelected) {
        self.titleLabel.textColor = self.norColor;
        self.backgroundColor = self.selColor;
    } else {
        self.titleLabel.textColor = self.selColor;
        self.backgroundColor = self.norColor;
    }
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (_isSelected) {
        self.titleLabel.textColor = self.norColor;
        self.backgroundColor = self.selColor;
    } else {
        self.titleLabel.textColor = self.selColor;
        self.backgroundColor = self.norColor;
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.isSelected) {
        [self moreAction];
    }
    if (_delegate) {
        [_delegate ItemStateChanged:self index:self.index isSelected:self.isSelected stubObject:self.stubObject];
    }
    
}

#pragma mark-Lazy
-(CALayer *)redPointLayer {
    if(!_redPointLayer) {
        _redPointLayer = [[CALayer alloc]init];
        _redPointLayer.backgroundColor = cRedColor.CGColor;
        CGFloat redPointX = self.bounds.size.width - 16;
        CGFloat redPointY = 4;
        _redPointLayer.frame = CGRectMake(redPointX, redPointY, 8, 8);
        _redPointLayer.cornerRadius = 4.0f;
        _redPointLayer.masksToBounds = YES;
        [self.layer addSublayer:_redPointLayer];
        _redPointLayer.hidden = YES;
    }
    return _redPointLayer;
}

-(NSString *)title {
    return self.titleLabel.text;
}

-(void)showRedPoint {
    self.redPointLayer.hidden = NO;
}

-(void)hideRedPoint {
    self.redPointLayer.hidden = YES;
}

@end
